// const request = require("request");
const fs = require("fs");
const http = require("https");
// const https = require("https");

// https://car3.autoimg.cn/cardfs/product/g3/M03/52/C5/240x180_0_q95_c42_autohomecar__ChsEkV3OyZeAQ9BNAAmJbmTysHw638.jpg
// let url = "https://car3.autoimg.cn/cardfs/product/g3/M03/3F/57/240x180_0_q95_c42_autohomecar__ChsEm13OyYWAd21UAAhLY1B-4Nk842.jpg";

// request(url).pipe(fs.createWriteStream("./img/2.jpg"))

// var server = http.createServer(function(req, res){}).listen(50082);

// console.log("http start");

// http.get(url,(res)=>{

//     res.setEncoding("binary");
//     let imgData = "";
//     res.on("data",chunk=>{
//         imgData+= chunk
//     })
//     res.on("end",()=>{
//         fs.writeFile("./img/1.jpg",imgData,"binary",(err)=>{
//             console.log(err);
//         });
//     })
// }).on("error",(err)=>{
//     console.log(err);
// })
function loadImg(url,path){
    http.get(url,(res)=>{
        const { statusCode } = res;

        if(statusCode !== 200) return 
        res.setEncoding("binary")
        let imgData = "";
        res.on("data",(chunk)=>{
            imgData += chunk
        })
        res.on("end",()=>{
            fs.writeFile(path,imgData,"binary",()=>{})
        })
    })
};

module.exports = loadImg